function [indmat,temp_cell] =cr_indmat(dist,lcone,lctwo,indp,lbnd,ubnd,alphap,betap,cofname )
% =========================================================================
% CR_INDMAT.M 
%
% Create the INDMAT matrix to use in transformations with CSMINWEL 
% For the case of the UNIFORM, the bounds [a,b] are given in 
% ALPHAP and BETAP 
%
% 
% Function must be called with the complete dist,lcone and lctwo vectors,
% i.e. including for the calibrated variables 
% 
% Alejandro Justiniano 11/16/2005 
% Set offset to 0 5/30/2008, prevents problems with offset in FMINCON 
% 1/13/2009 Added T density. Bounds will be given by LBND and UBND 
% 4/15/2009  All densities to 1, i.e. use bounds. 
% =======================================================================
nump = length( cofname ); 
if length( dist ) ~= nump 
    error('DIST size and COEFNAME size do not match') 
end 

temp =[lcone lctwo lbnd ubnd alphap betap]; 
if size( temp , 1 ) ~= nump;
    error('Wrong dimension inputs in CR_INDMAT.m')
end

% Create indmat matrix 
indmat=zeros(nump,4);
indmat(:,end) = ones(nump,1); 

offset=0; 
for jj=1:nump;
    
    cdist=deblank(upper(dist(jj)));
    
    if strcmp(cdist,'B')==1 
        indmat(jj,1)=1;
        indmat(jj,2)=max(offset,lbnd(jj)) ;
        indmat(jj,3)=min(1-offset,ubnd(jj)) ; 
    elseif strcmp(cdist,'T')==1 
        % This is the new Beta Truncated density 
        % Bounds are explicitly given 
        % It is different from uniform as it has BOTH 
        % ALPHA,BETA and LB UB parameters to define it 
        indmat(jj,1)=1;
        indmat(jj,2)=lbnd(jj)+offset ;
        indmat(jj,3)=ubnd(jj)-offset ;
    elseif strcmp( cdist , 'N' ) == 1         
        indmat(jj,1)=1;
        indmat(jj,2)=lbnd(jj)+offset;
        indmat(jj,3)=ubnd(jj)-offset;        
    elseif  strcmp( cdist, 'U' ) == 1 
        indmat(jj,1) =1; 
        indmat(jj,2) =alphap(jj) + offset; 
        indmat(jj,3) =betap(jj)  - offset;                 
    elseif strcmp( cdist , 'C' ) == 1         
        indmat(jj,1)=0;
        indmat(jj,2)=lcone(jj,1);
        indmat(jj,3)=0;        
    elseif ( strcmp(cdist,'G')==1 || strcmp(cdist,'W') ==1 || strcmp(cdist,'I') == 1 || strcmp(cdist,'E') == 1);
        indmat(jj,1)=1;
        indmat(jj,2) =lbnd(jj) + offset; 
        indmat(jj,3) =ubnd(jj)  - offset;   
    else        
        error('No distrubtion recognized');
    end
end
       
% Check all parameters to be estimated have a transformation 
chh1=find( indmat(indp,1) ~=1 & indmat(indp,1) ~= 2 ) ; 
if sum(chh1) > 0; 
    chh1 
    error('Need assign a transformation for parameter to be estimated')
end

% Check all calibrated parameters do not have a transformation 
ch = find( lctwo == 0 ); 
if any( indmat(ch,1) ~= 0 ); 
    error('Calibrated parameters have been assigned a 0,1 transformation') 
end 

% Checked all transformed parameters have a minimum interval length 
indint=find( indmat(:,1) == 1); 
chh=abs ( indmat(indint,2) - indmat(indint,3) ); 
chh2=find( chh < 0.1 ); 
if sum(chh2) > 0;
    chh2
    warning('Small interval in the transformation of a coefficient' )
    inp=input( 'Wish to Proceed? (Y/N)','s');    
    if upper(inp)=='N'
       error(' End Run') 
    end
end 

% Print the transformation matrix 
temp_cell1 = num2cprec( indmat(:,1) , 1 ); 
temp_cell2 = num2cprec( indmat(:,2:end), 4 ); 
temp_cell  = [cofname temp_cell1 temp_cell2]; 
disp('Transformation matrix from CR_INDMAT.M' ) 
printcell( temp_cell ); 